<template>
	<view>
		<view class="cu-bar bg-white">
			<view class="action border-title">
				<text class="cuIcon-titles text-green"></text>
				<text class="text-xl text-bold">{{title}}</text>
			</view>
		</view>
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">名称</view>
				<view> {{ whitelistInfo.name  || ''}} </view>
			</view>
			<view class="cu-form-group">
				<view class="title">供应商简称</view>
				<view> {{ whitelistInfo.simpleName  || ''}} </view>
			</view>
			<view class="cu-form-group">
				<view class="title">联系人</view>
				<view> {{ whitelistInfo.linkMan || '' }} </view>
			</view>
			<view class="cu-form-group">
				<view class="title">联系电话</view>
				<view> {{ whitelistInfo.tel  || ''}} </view>
			</view>
			<view class="cu-form-group">
				<view class="title">联系地址</view>
				<view> {{ whitelistInfo.address || '' }} </view>
			</view>
		</form>
		<view class="box">

			<view class="cu-bar bg-white">
				<view class="action border-title">
					<text class="cuIcon-titles text-green"></text>
					<text class="text-xl text-bold">文件列表</text>
				</view>
			</view>
			<view class="cu-list menu sm-border card-menu" v-for="(item, index) in whitelistInfo.fileList" :key="index">
				<!-- 多行Item -->
				<view class="cu-item">
					<view class="content padding-tb-sm">
						<view>
							<text class="cuIcon-link text-blue margin-right-xs"></text> {{item.fileName}}
						</view>
						<!-- <view class="text-gray text-sm">
							<text class="cuIcon-rechargefill margin-right-xs"></text> {{item.price}}
						</view> -->
					</view>
					<view class="action">
						<button class="cu-btn round" @click="downloadFile(item)">查看</button>
					</view>
				</view>
			</view>
		</view>
		<view class="box">
			<view class="cu-bar bg-white">
				<view class="action border-title">
					<text class="cuIcon-titles text-green"></text>
					<text class="text-xl text-bold">计划承接公司产品</text>
				</view>
			</view>
			<view class="cu-list menu sm-border card-menu" v-for="(item, index) in whitelistInfo.itemList" :key="index">
				<!-- 多行Item -->
				<view class="cu-item" @click="toDetail(item)">
					<view class="content padding-tb-sm">
						<view>
							{{item.itemCode}}
						</view>
						<view class="text-gray text-sm">
							{{item.itemName}}{{item.specification}}
						</view>
					</view>
					<view class="action">
						{{item.unit}}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "Whitelist",
		data() {
			return {
				whitelistInfo: {},
				title: "供应商白名单详情",
				item: {}
			};
		},
		props: {
			forms: {
				type: Object,
				default: () => {
					return {}
				}
			},
			value: {
				type: Object,
				default: () => {
					return {}
				}
			},
		},
		mounted() {
			uni.setNavigationBarTitle({
				title: '供应商白名单详情'
			});
			this.whitelistInfo = this.forms
		},
		methods: {
			toDetail(item) {
				this.$mRouter.push({
					route: `/pages/works/detail/materialDetail/materialDetail?id=${item.itemId}`
				})
			},
			// 下载文件
			downloadFile(opts) {
				let {
					url
				} = opts;
				// if (params) url = url + JSON.stringify(params);
				const token = uni.getStorageSync('accessToken');
				uni.downloadFile({
					url,
					// timeout: 30000, 超时时间
					header: {
						token
					}, // 下载文件需要传递的请求头参数都设置在 header 对象中
					success: res => {
						const {
							statusCode,
							tempFilePath // 临时文件路径，下载后的文件会存储到一个临时文件
						} = res;
						console.log(tempFilePath);
						if (statusCode === 200) {
							this.saveFile(tempFilePath);
							// if (operType === 'saveImg') {
							// 	this.saveImg(tempFilePath);
							// }
							// if (operType === 'saveFile') {
							// 	// this.saveFile(tempFilePath);
							// 	this.openFile(tempFilePath);
							// }
						} else {
							uni.showToast({
								title: '下载文件失败'
							});
						}
					}
				})
			},
			// 保存文件
			saveFile(filePath) {
				uni.saveFile({
					tempFilePath: filePath,
					success: (res) => {
						uni.showToast({
							title: '文件保存成功'
						});
						// console.log('文件保存成功，路径为：' + plus.io.convertLocalFileSystemURL(res.savedFilePath));
					},
					fail: (err) => {
						console.log('文件保存失败：' + err);
					}
				})
			},
			// 打开文档
			openFile(filePath) {
				let platform = uni.getSystemInfoSync().platform; //当前环境
				if (platform == 'ios') {
					filePath = escape(filePath);
				}
				uni.openDocument({
					filePath,
					showMenu: true,
					success: function(res) {
						console.log('打开文档成功');
					},
					fail: function(e) {
						console.log(e);
						uni.showToast({
							title: '打开失败'
						})
					}
				});
			},
		}
	}
</script>

<style scoped>
	.box {
		margin-top: 20upx;
	}
</style>